﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.Graphics;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Medicine.Activities;

namespace Medicine.CustomDialogs
{
    public class ShowMedicineDetailDialog : Android.App.Dialog
    {
        private Models.Medicine _medicine;
        public ShowMedicineDetailDialog([NotNull] Context context)
            : base(context)
        {

        }
        public ShowMedicineDetailDialog([NotNull] Context context, Models.Medicine medicine)
            : base(context)
        {
            _medicine = medicine;
        }

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.ShowMedicineDetailLayout);

            //设置弹窗界面的宽度
            var manager = Window.WindowManager;
            var display = manager.DefaultDisplay;
            var p = Window.Attributes;
            Point size = new Point();
            display.GetSize(size);
            //设置宽度为App的80%
            p.Width = (int)(size.X * 0.8);
            Window.Attributes = p;

            SetEvents();
            SetShowValues();
        }

        private void SetEvents()
        {
            var btn = FindViewById<Button>(Resource.Id.bt_confirm);
            btn.Click += (s, e) =>
            {
                this.Dismiss();
            };

            var upBtn = FindViewById<Button>(Resource.Id.bt_update);
            upBtn.Click += (s, e) =>
            {
                var intent = new Intent();
                intent.SetClass(this.Context, typeof(UpdateActivity));
                intent.PutExtra("medicine", Newtonsoft.Json.JsonConvert.SerializeObject(this._medicine));
                this.Context.StartActivity(intent);
                this.Dismiss();
            };
        }
        private void SetShowValues()
        {
            if (this._medicine != null)
            {
                var mType = this._medicine.GetType();
                var properties = mType.GetProperties();
                var resource = typeof(Resource.Id);
                var t = resource.GetFields();

                foreach (var item in properties)
                {
                    var txtName = $"txt_Show_{item.Name}";
                    var txtResource = t.FirstOrDefault(s => s.Name == txtName);
                    if (txtResource == null)
                        continue;

                    var tmpObj = resource.Assembly.CreateInstance(txtResource.ReflectedType.Name);
                    var txtId = txtResource.GetValue(tmpObj);
                    if (txtId != null)
                    {
                        int tmpTxtId = Convert.ToInt32(txtId);
                        TextView dateTxt = FindViewById<TextView>(tmpTxtId);
                        if (dateTxt != null)
                        {
                            var tmpValue = item.GetValue(this._medicine);
                            if (tmpValue == null)
                                dateTxt.Text = "";
                            else if(tmpValue is DateTime)
                            {
                                dateTxt.Text =Convert.ToDateTime(tmpValue).ToString("yyyy-MM-dd");
                            }
                            else
                                dateTxt.Text = tmpValue.ToString();
                        }
                    }
                }
            }
        }
    }
}